package servlet;

import com.ncist.dao.MessagesDao;
import com.ncist.pojo.Messages;
import com.ncist.utils.MybatisUtils;
import org.apache.ibatis.session.SqlSession;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
@WebServlet("/message")
public class messagesServlet extends HttpServlet {
    SqlSession sqlSession;
    @Override
    public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        sqlSession = MybatisUtils.getSqlSession();
        MessagesDao mapper = sqlSession.getMapper(MessagesDao.class);
        List<Messages> messages = mapper.getAllMessages();
        request.setAttribute("messages", messages);
        request.getRequestDispatcher("/index.jsp").forward(request, response);
        //第一步：获得sqlSession对象
        //第二步:获得Mapper接口的代理对象
        //第三步:执行方法
        //关闭sqlSession
        sqlSession.close();

    }

    @Override
    public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }
}